package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.RoomInfo;
import com.atguigu.model.enums.ReleaseStatus;
import com.atguigu.webadmin.service.RoomInfoService;
import com.atguigu.webadmin.vo.room.RoomDetailVo;
import com.atguigu.webadmin.vo.room.RoomItemVo;
import com.atguigu.webadmin.vo.room.RoomQueryVo;
import com.atguigu.webadmin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
@Slf4j
@CrossOrigin
public class RoomController {

    @Autowired
    private RoomInfoService roomInfoService;

    @Operation(summary = "保存或更新房间信息")
    @Parameters({
            @Parameter(name = "roomSubmitVo",description = "提交的房间信息vo对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Object> saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        log.info("{}", roomSubmitVo);
        roomInfoService.saveOrUpdateRoom(roomSubmitVo);
        return Result.ok("保存或更新成功");
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "size",description = "展示数量",in = ParameterIn.QUERY),
            @Parameter(name = "queryVo",description = "查询的房间列表vo对象",in = ParameterIn.DEFAULT)
    })
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        //根据页码和数量查询分页信息
        IPage<RoomItemVo> page = new Page<>(current, size);
        //封装分页查询到的信息
        IPage<RoomItemVo> result = roomInfoService.pageRoomItemByQuery(page, queryVo);
        return Result.ok(result);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @Parameters({
            @Parameter(name = "id",description = "房间id",in = ParameterIn.DEFAULT)
    })
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailById(id);
        log.info("{}",roomDetailVo);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @Parameters({
            @Parameter(name = "id",description = "房间id",in = ParameterIn.QUERY)
    })
    @DeleteMapping("removeById")
    public Result<Object> removeRoomById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @Parameters({
            @Parameter(name = "id",description = "房间id",in = ParameterIn.QUERY),
            @Parameter(name = "status",description = "房间发布状态",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateReleaseStatusById")
    public Result<Object> updateReleaseStatusById(Long id, ReleaseStatus status) {
        //根据id修改房间发布状态
        LambdaUpdateWrapper<RoomInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //指定更新条件
        lambdaUpdateWrapper.eq(id != null,RoomInfo::getApartmentId,id);
        //设置更新属性
        lambdaUpdateWrapper.set(RoomInfo::getIsRelease,status);
        roomInfoService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Parameters({
            @Parameter(name = "id",description = "房间id",in = ParameterIn.QUERY)
    })
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        //条件查询
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        //指定条件
        queryWrapper.eq(RoomInfo::getApartmentId,id);
        //指定查询的房间状态为已发布
        queryWrapper.eq(RoomInfo::getIsRelease,ReleaseStatus.RELEASED);
        //执行查询
        List<RoomInfo> list = roomInfoService.list(queryWrapper);
        log.info("{}",list);
        return Result.ok(list);
    }

}


















